import config from 'config';
import { AgentConfig, OSSLogPolicy, RoleType } from './type';

function getAgentConfig(): AgentConfig {
  const identity: string = process.env.AGENT_IDENTITY || config.get('agent.identity');
  const advertiseIp: string = process.env.AGENT_ADVERTISEIP || config.get('agent.advertiseIp');
  const modelCleanup: boolean = process.env.AGENT_MODELCLEANUP ? process.env.AGENT_MODELCLEANUP !== 'false' :
    config.has('agent.modelCleanup') ? config.get('agent.modelCleanup') : true;
  const failedJobCleanup: boolean = config.has('agent.failedJobCleanup') ? config.get('agent.failedJobCleanup') : false;
  const schemaValidation: boolean = config.has('agent.schemaValidation') ? config.get('agent.schemaValidation') : true;

  const gatewayHost: string = process.env.GATEWAY_HOST || config.get('gateway.host');
  const gatewayPort: number = process.env.GATEWAY_PORT ? parseInt(process.env.GATEWAY_PORT) : config.get('gateway.port');
  
  const ossPolicy: OSSLogPolicy = config.has('log.oss.policy') ? config.get('log.oss.policy') : 'all';

  const flushInterval: number = config.has('log.oss.flushInterval') ? config.get('log.oss.flushInterval') : 10000;
  const flushMaxItems: number = config.has('log.oss.flushMaxItems') ? config.get('log.oss.flushMaxItems') : 5;

  const role: RoleType = config.has('agent.role') ? config.get('agent.role') : 'public';
  const password: string = config.has('agent.password') ? config.get('agent.password') : '';

  const storageCLIEnable: boolean = config.has('storage.cli.enable') ? config.get('storage.cli.enable') : false;

  const osInfoEnable: boolean = config.has('os.info.enable') ? config.get('os.info.enable') : false;
  const osInfoInterval: number = config.has('os.info.interval') ? config.get('os.info.interval') : 60000;

  return { identity, advertiseIp, modelCleanup, failedJobCleanup, schemaValidation, gatewayHost, gatewayPort, ossPolicy, flushInterval, flushMaxItems, role, password, storageCLIEnable, osInfoEnable, osInfoInterval };
}

export const agentConfig = getAgentConfig();